<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/25
 * Time: 16:08
 */

namespace Common\Model;

/**
 * 商品分类缓存模型
 * Class ProductCateModel
 * @package Common\Model
 */
class ProductCateModel
{
    private $cacheKey = 'wxshop_all_product_cate_detail_';//所有的商品分类详情
    private $store_product_cate_key = 'wxshop_store_product_cate_';//店铺的商品分类列表
    private $store_product_cate_list_key = 'wxshop_store_product_cate_list';

    public function clearCache($uid,$id){
        $this->_buildCache($uid,$id);
    }

    private function _buildCache($uid,$id){
        !empty($uid) && S($this->store_product_cate_key.$uid,null);
        !empty($id) && S($this->cacheKey.$id,null);
        S($this->store_product_cate_list_key,null);
    }

    /**
     * 获取所有的商品分类
     * @return array
     */
    public function getAllProductCate(){
        $list = S($this->store_product_cate_list_key);
        if(!$list){
            $list = M('project_cate')->select();
            S($this->store_product_cate_list_key,$list);
        }
        return $list;
    }

    /**
     * 获取商品分类详情
     * @param $cate_id
     * @return array|bool
     */
    public function detail($cate_id,$field=true){
        if(empty($cate_id)) return false;

        $detail = S($this->cacheKey.$cate_id);
        if(!$detail){
            $detail = M('project_cate')->where('id='.$cate_id)->find();
            S($this->cacheKey.$cate_id,$detail);
        }
        if($field === true){
            return $detail;
        }else if($field){
            return $detail[$field];
        }
    }

    /**
     * 获取某店铺所有的商品分类列表
     * @param $store_id
     */
    public function getAllStoreProductCate($store_id){
        if(empty($store_id)) return array();

        $data = S($this->store_product_cate_key.$store_id);
        if(!$data){
            $data = M('project_cate')->where('uid='.$store_id)->select();
            S($this->store_product_cate_key.$store_id,$data);
        }
        return $data;
    }

    public function getStoreProductCate($shop_id){
        if(empty($shop_id)) return false;
        $list = M('project_cate')->where('shop_id='.$shop_id)->field('id,title')->select();
        return $list;
    }
}